package com.kanshu.ksgb.fastread.doudou.base.baseui;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.net.bean.BaseResult;
import com.kanshu.ksgb.fastread.doudou.common.net.rx.BaseResultData;
import com.kanshu.ksgb.fastread.doudou.common.util.NetUtils;

import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

public class BaseFragment extends Fragment {
    protected View mFragView;
    protected Context mContext;
    protected boolean isVisible;
    protected boolean isFetchData;
    protected boolean isPrepared;
    protected Subject<Integer> lifeCyclerSubject = PublishSubject.create();

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d("wcy", "basefragment:" + "onCreateView");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.d("wcy", "basefragment:" + "onActivityCreated");
        isPrepared = true;
        loadData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        lifeCyclerSubject.onNext(BaseActivity.ACTION_DESTORY);
    }

    protected boolean noNet() {
        return !NetUtils.isNetworkAvailable(mContext);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        Log.d("wcy", "basefragment:" + "setUserVisibleHint");
        if (getUserVisibleHint()) {
            isVisible = true;
            loadData();
        } else {
            isVisible = false;
        }
    }

    protected void updateViews() {
    }

    private void loadData() {
        if (!isPrepared || !isVisible || isFetchData) {
            return;
        }
        updateViews();
        isFetchData = true;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        GlideImageLoader.clearMemory(getActivity());
    }

    public <T> ObservableTransformer<T, T> asyncRequest() {
        return upstream -> upstream.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .takeUntil(lifeCyclerSubject);
    }

    public <T> ObservableTransformer<BaseResult<T>, T> asyncRequestData() {
        return upstream -> upstream.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .takeUntil(lifeCyclerSubject).map(new BaseResultData<T>());
    }

}
